const toggleBatchBtn = document.querySelector('#toggleBatchBtn')
const ul = document.querySelector('#batch')
const map = document.querySelector('.map')
const pie = document.querySelector('.pie')

getStudentOverView();
getStudentInfo()

//#region 渲染左上角数据
function getStudentOverView() {
  axios
    .get('/student/overview', {})
    .then((result) => {
      let obj = result.data.data
      for (let key in obj) {
        document.querySelector(`.${key}`).innerText = obj[key]
      }
    });
}
//#endregion


//#region 获取学生信息、渲染薪资
function getStudentInfo() {
  let students;
  axios.get('/student/list').then(result => {
    students = result.data.data;

    renderSalary(students)
    getScoreByBatch(1)
    renderMap(students)
    renderProvince(students)
  })
}
//#endregion

//#region 渲染薪资图表
function renderSalary(arr) {
  let nameList = arr.map(v => v.name)
  let trueSalaryList = arr.map(v => v.truesalary)
  let expSalaryList = arr.map(v => v.salary)

  const salaryDom = document.querySelector('.panel.line')
  // 基于准备好的dom，初始化echarts实例
  const myChart = echarts.init(salaryDom);

  // 指定图表的配置项和数据
  const option = {
    color: ['red', 'blue'],
    title: {
      text: '薪资 Salary',
    },

    legend: {
      data: ['期望薪资', '实际薪资'],
    },
    xAxis: {
      data: nameList,
    },
    yAxis: {},
    series: [{
      name: '期望薪资',
      type: 'line',
      smooth: true,
      data: expSalaryList,
      symbol: 'none',
    }, {
      name: '实际薪资',
      type: 'line',
      smooth: true,
      data: trueSalaryList,
      symbol: 'none',
    }],
    dataZoom: [{
      start: 0,
      end: 13
    }]
  };
  // 使用刚指定的配置项和数据显示图表。
  myChart.setOption(option);
}
//#endregion

//#region 根据次数获取成绩
function getScoreByBatch(index) {
  axios.get('/score/batch?batch=' + index).then(result => {
    const data = result.data.data
    if (result.data.code === 0) {
      //调用渲染成绩图表
      renderScore(data)
    } else {
      toastr.error(result.data.message)
    }
  })
}

//#region 渲染分数图表
function renderScore(data) {
  const dom = document.querySelector('.barChart')
  const myChart = echarts.init(dom);
  // const data = {
  //   group: [1, 6, 8, 2, 3, 4, 5, 7],
  //   avgScore: [77, 80, 70, 66, 83, 67, 67, 60],
  //   lt60: [1, 1, 1, 1, 0, 2, 1, 2],
  //   gt60: [1, 0, 2, 2, 2, 1, 1, 1],
  //   gt80: [3, 3, 1, 1, 2, 1, 1, 1],
  // };

  const option = {
    // 网格 设置 图表 在 div空间内的大小
    grid: {
      // 拉伸图表距离顶部多少空间
      top: 20,
      // 拉伸图表距离底部多少空间
      bottom: 20
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999',
        },
      },
    },
    legend: {
      data: ['平均分', '低于60分', '60-80分', '高于80分人数'],
    },
    xAxis: [{
      type: 'category',
      data: data.group,
      axisPointer: {
        type: 'shadow',
      },
    }, ],
    yAxis: [{
        type: 'value',
        min: 0, // 最小值
        max: 100, // 最大值
        interval: 10, // 间隔
        axisLabel: {
          formatter: '{value} 分',
        },
      },
      {
        type: 'value',
        min: 0,
        max: 10,
        interval: 1,
        axisLabel: {
          formatter: '{value} 人',
        },
      },
    ],
    series: [{
        name: '平均分',
        type: 'bar',
        yAxisIndex: 0, // 第0个y轴刻度
        tooltip: {
          valueFormatter: function (value) {
            return value + ' ml';
          },
        },
        data: data.avgScore, // 平均分
      },
      {
        name: '低于60分',
        type: 'bar',
        yAxisIndex: 1, // 第1个y轴刻度
        tooltip: {
          valueFormatter: function (value) {
            return value + ' ml';
          },
        },
        data: data.lt60, // 低于60分
      },
      {
        name: '60-80分',
        type: 'bar',
        yAxisIndex: 1, // 第1个y轴刻度
        tooltip: {
          valueFormatter: function (value) {
            return value + ' ml';
          },
        },
        data: data.gt60, // 60 - 80
      },
      {
        name: '高于80分人数',
        type: 'bar',
        yAxisIndex: 1, // 第1个y轴刻度
        tooltip: {
          valueFormatter: function (value) {
            return value + ' ml';
          },
        },
        data: data.gt80,
      },
    ],
  };
  myChart.setOption(option);
}
//#endregion

//#region 分数图标菜单点击事件
toggleBatchBtn.addEventListener('click', function () {
  ul.classList.toggle('show')

})
ul.addEventListener('click', function (e) {
  const index = e.target.dataset.index
  console.log('index: ', index);
  getScoreByBatch(index)
})
//#endregion




function renderProvince(data) {
  let provinces = data.map(v => v.province)


  let newArr = []
  provinces.forEach(value => {
    const index = newArr.findIndex(v => v.name === value)
    if (index === -1) {
      //没找到
      newArr.push({
        name: value,
        value: 1
      })
    } else {
      newArr[index].value++
    }
  })
  console.log(newArr);



  const myChart = echarts.init(pie);
  const option = {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b} : {c} ({d}%)'
    },
    toolbox: {
      show: false,
      feature: {
        mark: {
          show: true
        },
        dataView: {
          show: true,
          readOnly: false
        },
        restore: {
          show: true
        },
        saveAsImage: {
          show: true
        }
      }
    },
    series: [{
      name: 'Nightingale Chart',
      type: 'pie',
      radius: [20, 200],
      center: ['50%', '50%'],
      roseType: 'area',
      itemStyle: {
        borderRadius: 8
      },
      data: newArr
    }]
  };
  myChart.setOption(option);
}


renderMap()

function renderMap() {

  const myChart = echarts.init(document.querySelector('.map'));
  /**
  此版本通过设置geoindex && seriesIndex: [1] 属性来实现geo和map共存，来达到hover散点和区域显示tooltip的效果
  
  默认情况下，map series 会自己生成内部专用的 geo 组件。但是也可以用这个 geoIndex 指定一个 geo 组件。这样的话，map 和 其他 series（例如散点图）就可以共享一个 geo 组件了。并且，geo 组件的颜色也可以被这个 map series 控制，从而用 visualMap 来更改。
  当设定了 geoIndex 后，series-map.map 属性，以及 series-map.itemStyle 等样式配置不再起作用，而是采用 geo 中的相应属性。
  
  http://echarts.baidu.com/option.html#series-map.geoIndex
  
  并且加了pin气泡图标以示数值大小
  */
  var points = [{
    value: [118.8062, 31.9208],
    itemStyle: {
      color: '#4ab2e5'
    }
  }, {
    value: [127.9688, 45.368],
    itemStyle: {
      color: '#4fb6d2'
    }
  }, {
    value: [110.3467, 41.4899],
    itemStyle: {
      color: '#52b9c7'
    }
  }, {
    value: [125.8154, 44.2584],
    itemStyle: {
      color: '#5abead'
    }
  }, {
    value: [116.4551, 40.2539],
    itemStyle: {
      color: '#f34e2b'
    }
  }, {
    value: [123.1238, 42.1216],
    itemStyle: {
      color: '#f56321'
    }
  }, {
    value: [114.4995, 38.1006],
    itemStyle: {
      color: '#f56f1c'
    }
  }, {
    value: [117.4219, 39.4189],
    itemStyle: {
      color: '#f58414'
    }
  }, {
    value: [112.3352, 37.9413],
    itemStyle: {
      color: '#f58f0e'
    }
  }, {
    value: [109.1162, 34.2004],
    itemStyle: {
      color: '#f5a305'
    }
  }, {
    value: [103.5901, 36.3043],
    itemStyle: {
      color: '#e7ab0b'
    }
  }, {
    value: [106.3586, 38.1775],
    itemStyle: {
      color: '#dfae10'
    }
  }, {
    value: [101.4038, 36.8207],
    itemStyle: {
      color: '#d5b314'
    }
  }, {
    value: [103.9526, 30.7617],
    itemStyle: {
      color: '#c1bb1f'
    }
  }, {
    value: [108.384366, 30.439702],
    itemStyle: {
      color: '#b9be23'
    }
  }, {
    value: [113.0823, 28.2568],
    itemStyle: {
      color: '#a6c62c'
    }
  }, {
    value: [102.9199, 25.46639],
    itemStyle: {
      color: '#96cc34'
    }
  }, {
    value: [119.4543, 25.9222]
  }]





  echarts.registerMap('china', geoJson);
  myChart.hideLoading();
  option = {
    backgroundColor: 'transparent',


    geo: {
      map: 'china',
      aspectScale: 0.75, //长宽比
      zoom: 1.1,
      roam: false,
      itemStyle: {
        normal: {
          areaColor: {
            type: 'radial',
            x: 0.5,
            y: 0.5,
            r: 0.8,
            colorStops: [{
              offset: 0,
              color: '#09132c' // 0% 处的颜色
            }, {
              offset: 1,
              color: '#274d68' // 100% 处的颜色
            }],
            globalCoord: true // 缺省为 false
          },
          shadowColor: 'rgb(58,115,192)',
          shadowOffsetX: 10,
          shadowOffsetY: 11
        },
        emphasis: {
          areaColor: '#2AB8FF',
          borderWidth: 0,
          color: 'green',
          label: {
            show: false
          }
        }
      },
      regions: [{
        name: '南海诸岛',
        itemStyle: {
          areaColor: 'rgba(0, 10, 52, 1)',

          borderColor: 'rgba(0, 10, 52, 1)',
          normal: {
            opacity: 0,
            label: {
              show: false,
              color: "#009cc9",
            }
          }
        },


      }],
    },
    series: [{
        type: 'map',
        roam: false,
        label: {
          normal: {
            show: true,
            textStyle: {
              color: '#1DE9B6'
            }
          },
          emphasis: {
            textStyle: {
              color: 'rgb(183,185,14)'
            }
          }
        },

        itemStyle: {
          normal: {
            borderColor: 'rgb(147, 235, 248)',
            borderWidth: 1,
            areaColor: {
              type: 'radial',
              x: 0.5,
              y: 0.5,
              r: 0.8,
              colorStops: [{
                offset: 0,
                color: '#09132c' // 0% 处的颜色
              }, {
                offset: 1,
                color: '#274d68' // 100% 处的颜色
              }],
              globalCoord: true // 缺省为 false
            },
          },
          emphasis: {
            areaColor: 'rgb(46,229,206)',
            //    shadowColor: 'rgb(12,25,50)',
            borderWidth: 0.1
          }
        },
        zoom: 1.1,
        //     roam: false,
        map: 'china' //使用
        // data: this.difficultData //热力图数据   不同区域 不同的底色
      }, {
        type: 'effectScatter',
        coordinateSystem: 'geo',
        showEffectOn: 'render',
        zlevel: 1,
        rippleEffect: {
          period: 15,
          scale: 4,
          brushType: 'fill'
        },
        hoverAnimation: true,
        label: {
          normal: {
            formatter: '{b}',
            position: 'right',
            offset: [15, 0],
            color: '#1DE9B6',
            show: true
          },
        },
        itemStyle: {
          normal: {
            color: '#1DE9B6'
              /* function (value){ //随机颜色
                 return "#"+("00000"+((Math.random()*16777215+0.5)>>0).toString(16)).slice(-6);
                 }*/
              ,
            shadowBlur: 10,
            shadowColor: '#333'
          }
        },
        symbolSize: 12,
        data: points
      }, //地图线的动画效果
      {
        type: 'lines',
        zlevel: 2,
        effect: {
          show: true,
          period: 4, //箭头指向速度，值越小速度越快
          trailLength: 0.4, //特效尾迹长度[0,1]值越大，尾迹越长重
          symbol: 'arrow', //箭头图标
          symbolSize: 7, //图标大小
        },
        lineStyle: {
          normal: {
            color: '#1DE9B6'
              /* function (value){ //随机颜色
                          
                          ['#f21347','#f3243e','#f33736','#f34131','#f34e2b',
                          '#f56321','#f56f1c','#f58414','#f58f0e','#f5a305',
                          '#e7ab0b','#dfae10','#d5b314','#c1bb1f','#b9be23',
                          '#a6c62c','#96cc34','#89d23b','#7ed741','#77d64c',
                          '#71d162','#6bcc75','#65c78b','#5fc2a0','#5abead',
                          '#52b9c7','#4fb6d2','#4ab2e5']
   return "#"+("00000"+((Math.random()*16777215+0.5)>>0).toString(16)).slice(-6);
   }*/
              ,
            width: 1, //线条宽度
            opacity: 0.1, //尾迹线条透明度
            curveness: .3 //尾迹线条曲直度
          }
        },
        data: [{
          coords: [
            [118.8062, 31.9208],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#4ab2e5'
          }
        }, {
          coords: [
            [127.9688, 45.368],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#4fb6d2'
          }
        }, {
          coords: [
            [110.3467, 41.4899],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#52b9c7'
          }
        }, {
          coords: [
            [125.8154, 44.2584],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#5abead'
          }
        }, {
          coords: [
            [116.4551, 40.2539],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#f34e2b'
          }
        }, {
          coords: [
            [123.1238, 42.1216],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#f56321'
          }
        }, {
          coords: [
            [114.4995, 38.1006],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#f56f1c'
          }
        }, {
          coords: [
            [117.4219, 39.4189],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#f58414'
          }
        }, {
          coords: [
            [112.3352, 37.9413],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#f58f0e'
          }
        }, {
          coords: [
            [109.1162, 34.2004],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#f5a305'
          }
        }, {
          coords: [
            [103.5901, 36.3043],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#e7ab0b'
          }
        }, {
          coords: [
            [106.3586, 38.1775],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#dfae10'
          }
        }, {
          coords: [
            [101.4038, 36.8207],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#d5b314'
          }
        }, {
          coords: [
            [103.9526, 30.7617],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#c1bb1f'
          }
        }, {
          coords: [
            [108.384366, 30.439702],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#b9be23'
          }
        }, {
          coords: [
            [113.0823, 28.2568],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#a6c62c'
          }
        }, {
          coords: [
            [102.9199, 25.46639],
            [119.4543, 25.9222]
          ],
          lineStyle: {
            color: '#96cc34'
          }
        }]
      }

    ]
  };
  myChart.setOption(option, true);

}